define(function (require) {
    require('validate');
    require('layer_ui');

    var Base = require('group/common/modal');
    var utils = require('common/utils/home');
    var Mask = require('common/mask');
    var mask = new Mask();
    var ModalTpl = require('text!./templates/item_attr_modal.html');

    return Base.extend({
        name: 'item_attr',
        initialize: function (params) {
            Base.prototype.initialize.apply(this, [params]);
        },
        show: function () {
            var _this = this;
            if (!_this.is_create()) {
                utils.ajaxGet({id: _global.item.id, a: 'ajax_item_sku'}, function (resp) {
                    var index = window.Z_INDEX + 2;
                    _this.info = resp.data;

                    _this.render_modal({
                        html: _this.render(ModalTpl, {
                            info: _this.info,
                            index: index
                        }),
                        name: _this.name,
                        index: index - 1
                    });
                    Base.prototype.show.apply(_this, []);
                    _this.bind_events();
                });
            }
            else {
                Base.prototype.show.apply(this, []);
            }
        },
        bind_events: function () {
            // 判断可不可以购买
            $.ajax({
                url:_.U({m: 'transaction',a: 'testOrder'}),
                type:'GET',
                data:{id: 1},
                dataType:'JSON',
                success:function(resp){
                    if(resp.status == 0){
                        layer.msg(resp.msg);
                        window.location.href = resp.url;
                    }
                }
            });

            var _this = this;
            _this.$('.js-choice-attr-close').click(function () {
                _this.hide();
            });
            _this.$('.J_submit').click(function () {
                var sku_id = _.intval(_this.$('#J_item_sku_list span.ac').data('id'));
                if (sku_id == 0 && _this.$('#J_item_sku_list span.no').size() < _this.$('#J_item_sku_list span').size()) {
                    mask.error('请选择规格!');
                    return;
                }
				var goods_number = _this.$('#J_goods_number').val();
				var user_price = _.floatval(_this.$('#J_user_price').html());
                var item_price = _.floatval(_this.$('#J_item_sku_price').html());

				if(user_price < item_price*goods_number){
					layer.confirm('余额不足,请充值!', {
						title: '提示',
						btn: ['去充值','关闭'] //按钮
					}, 
					function(){
						window.location.href = _.U({
							m: 'user',
							a: 'recharge',
						});
					}, function(){
						layer.close();
					});
                    return;
				}
                /*window.location.href = _.U({
                    m: 'transaction',
                    a: 'direct_buy',
                    sku_id: sku_id,
                    item_id: _global.item.id,
                    goods_number: goods_number
                });*/
                layer.confirm(_.sprintf('购买此商品将使用￥%.2f元,确定要购买吗?', item_price*goods_number), {title:'提示'},function () {
					$.ajax({
						url:_.U({m: 'transaction',a: 'create_order'}),
						type:'POST',
						data:{sku_id: sku_id,item_id: _global.item.id,goods_number: goods_number},
						dataType:'JSON',
						success:function(resp){
							if(resp.status == 0){
								layer.msg(resp.msg);
							}else{
								window.location.href = resp.data.url;
							}
						}
					});

                });
            });
            _this.$('#J_item_sku_list span').click(function () {
                var $this = $(this);
                if ($this.hasClass('no')) {
                    mask.error('暂时无货!');
                }
                if ($this.hasClass('no') || $this.hasClass('ac')) {
                    return;
                }
                $this.addClass('ac').siblings().removeClass('ac');
                _this.$('#J_item_sku_price').html($this.data('price'));
                _this.$('#J_item_sku_stock').html($this.data('stock'));
            });
            _this.$('#J_sku_num_plus').click(function () {
                var num = _.intval(_this.$('#J_goods_number').val());
                var stock = _.intval(_this.$('#J_item_sku_stock').html());
                var user_price = _.floatval(_this.$('#J_user_price').html());
                var item_price = _.floatval(_this.$('#J_item_sku_price').html());

                num = num + 1 > stock ? stock : num + 1;

                if (_this.info.score_maxs > 0) {
                    num = num > _this.info.score_maxs ? _this.info.score_maxs : num;
                }
                _this.$('#J_goods_number').val(num);

				if(user_price < item_price*num){
					$('#J_user_price_msg').html('余额不足');
				}else{
					$('#J_user_price_msg').html('');
				}
            });
            _this.$('#J_sku_num_minus').click(function () {
                var num = _.intval(_this.$('#J_goods_number').val());
				var user_price = _.floatval(_this.$('#J_user_price').html());
                var item_price = _.floatval(_this.$('#J_item_sku_price').html());
                num = num - 1 <= 0 ? 1 : num - 1;
                _this.$('#J_goods_number').val(num);
				if(user_price < item_price*num){
					$('#J_user_price_msg').html('余额不足');
				}else{
					$('#J_user_price_msg').html('');
				}
            });
        }
    });
});